module.exports = function ($scope, $resource, $modalInstance, bean, toaster, FileUploader) {
    $scope.pageInfo = {
        menu: {}
    };
    $scope.bean;
    $scope.url = '/api/as/mc/shopmenu/saveMyMenu'
    $scope.seeupdate = true;
    if (bean) {
        console.log(bean)
        $scope.bean = bean;
        $scope.seeupdate = bean.menu_update == 0 ? true : false;
        $scope.pageInfo.menu = {
            id: bean.menu_id,
            menu_url: bean.menu_url,
            menu_type: bean.menu_type,
            menu_name: bean.menu_name
        }
        $scope.url = '/api/as/mc/shopmenu/updateMyMenu'
    } else {
        $scope.bean = {
            menu_id: '',
            menu_url: '',
            menu_type: '',
            menu_name: '',
            menu_new_name: '',
            menu_update: '0',
            menu_state: '0',
            asort: 1,
            img: ''
        };
    }
    $resource('/api/as/mc/shopmenu/menuList', {}, {}).save({}, function (res) {
        if (res.errcode === 0) {
            $scope.menuAllList = res.data;
            $scope.menuMap = {};
            for (let index = 0; index < $scope.menuAllList.length; index++) {
                const element = $scope.menuAllList[index];
                $scope.menuMap[element.id] = element;
            }
        } else {
            toaster.error({ title: '', body: res.errmsg });
        }
    });

    $scope.changeMenu = function () {
        //不可修改
        $scope.bean.menu_id = $scope.pageInfo.menu.id;
        $scope.bean.menu_name = $scope.menuMap[$scope.pageInfo.menu.id].menu_name;
        $scope.bean.menu_url = $scope.menuMap[$scope.pageInfo.menu.id].menu_url;
        $scope.bean.menu_type = $scope.menuMap[$scope.pageInfo.menu.id].menu_type;
        //可以修改
        $scope.bean.img = $scope.menuMap[$scope.pageInfo.menu.id].img;
        $scope.bean.menu_new_name = $scope.menuMap[$scope.pageInfo.menu.id].menu_name;
        $scope.bean.menu_update = $scope.menuMap[$scope.pageInfo.menu.id].menu_update;
    }

    //添加
    $scope.ok = function () {
        $resource($scope.url, {}, {}).save($scope.bean, function (res) {
            if (res.errcode === 0) {
                toaster.success({ title: '', body: '操作成功' });
                $modalInstance.close();
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }

    //取消
    $scope.close = function () {
        $modalInstance.dismiss('cancel');
    }

    $scope.uploader = new FileUploader({
        url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=skv4&selfdir=mymenu'+ Date.parse(new Date())
    });

    $scope.uploader.filters.push({
        name: 'imageFilter',
        fn: function (item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });

    $scope.uploader.onSuccessItem = function (fileItem, response, status, headers) {
        $scope.bean.img = response.savename;
    };
};